home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_008 / src / makedefs.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  214 lines

  1. /* construct definitions of object constants */#define    DEF_FILE    "def.objects.h"
  2. #define    LINSZ    1000
  3. #define    STRSZ    40
  4.  
  5. int fd;
  6. char string[STRSZ];
  7.  
  8. main(){
  9. register int index = 0;
  10. register int propct = 0;
  11. register char *sp;
  12.     fd = open(DEF_FILE, 0);
  13.     if(fd < 0) {
  14.         perror(DEF_FILE);
  15.         exit(1);
  16.     }
  17.     skipuntil("objects[] = {");
  18.     while(getentry()) {
  19.         if(!*string){
  20.             index++;
  21.             continue;
  22.         }
  23.         for(sp = string; *sp; sp++)
  24.             if(*sp == ' ' || *sp == '\t')
  25.                 *sp = '_';
  26.         if(!strncmp(string, "RIN_", 4)){
  27.             capitalize(string+4);
  28.             printf("#define    %s    u.uprops[%d].p_flgs\n",
  29.                 string+4, propct++);
  30.         }
  31.         for(sp = string; *sp; sp++) capitalize(sp);
  32.         /* avoid trouble with stupid C preprocessors */
  33.         if(!strncmp(string, "WORTHLESS_PIECE_OF_", 19))
  34.             printf("/* #define %s    %d */\n", string, index);
  35.         else
  36.             printf("#define    %s    %d\n", string, index);
  37.         index++;
  38.     }
  39.     printf("\n#define    CORPSE    DEAD_HUMAN\n");
  40.     printf("#define    LAST_GEM    (JADE+1)\n");
  41.     printf("#define    LAST_RING    %d\n", propct);
  42.     printf("#define    NROFOBJECTS    %d\n", index-1);
  43. }
  44.  
  45. char line[LINSZ], *lp = line, *lp0 = line, *lpe = line;
  46. int eof;
  47.  
  48. readline(){
  49. register int n = read(fd, lp0, (line+LINSZ)-lp0);
  50.     if(n < 0){
  51.         printf("Input error.\n");
  52.         exit(1);
  53.     }
  54.     if(n == 0) eof++;
  55.     lpe = lp0+n;
  56. }
  57.  
  58. char
  59. nextchar(){
  60.     if(lp == lpe){
  61.         readline();
  62.         lp = lp0;
  63.     }
  64.     return((lp == lpe) ? 0 : *lp++);
  65. }
  66.  
  67. skipuntil(s) char *s; {
  68. register char *sp0, *sp1;
  69. loop:
  70.     while(*s != nextchar())
  71.         if(eof) {
  72.             printf("Cannot skipuntil %s\n", s);
  73.             exit(1);
  74.         }
  75.     if(strlen(s) > lpe-lp+1){
  76.         register char *lp1, *lp2;
  77.         lp2 = lp;
  78.         lp1 = lp = lp0;
  79.         while(lp2 != lpe) *lp1++ = *lp2++;
  80.         lp2 = lp0;    /* save value */
  81.         lp0 = lp1;
  82.         readline();
  83.         lp0 = lp2;
  84.         if(strlen(s) > lpe-lp+1) {
  85.             printf("error in skipuntil");
  86.             exit(1);
  87.         }
  88.     }
  89.     sp0 = s+1;
  90.     sp1 = lp;
  91.     while(*sp0 && *sp0 == *sp1) sp0++, sp1++;
  92.     if(!*sp0){
  93.         lp = sp1;
  94.         return(1);
  95.     }
  96.     goto loop;
  97. }
  98.  
  99. getentry(){
  100. int inbraces = 0, inparens = 0, stringseen = 0, commaseen = 0;
  101. int prefix = 0;
  102. char ch;
  103. #define    NSZ    10
  104. char identif[NSZ], *ip;
  105.     string[0] = string[4] = 0;
  106.     /* read until {...} or XXX(...) followed by ,
  107.        skip comment and #define lines
  108.        deliver 0 on failure
  109.      */
  110.     while(1) {
  111.         ch = nextchar();
  112.     swi:
  113.         if(letter(ch)){
  114.             ip = identif;
  115.             do {
  116.                 if(ip < identif+NSZ-1) *ip++ = ch;
  117.                 ch = nextchar();
  118.             } while(letter(ch) || digit(ch));
  119.             *ip = 0;
  120.             while(ch == ' ' || ch == '\t') ch = nextchar();
  121.             if(ch == '(' && !inparens && !stringseen)
  122.                 if(!strcmp(identif, "WAND") ||
  123.                    !strcmp(identif, "RING") ||
  124.                    !strcmp(identif, "POTION") ||
  125.                    !strcmp(identif, "SCROLL"))
  126.                 (void) strncpy(string, identif, 3),
  127.                 string[3] = '_',
  128.                 prefix = 4;
  129.         }
  130.         switch(ch) {
  131.         case '/':
  132.             /* watch for comment */
  133.             if((ch = nextchar()) == '*')
  134.                 skipuntil("*/");
  135.             goto swi;
  136.         case '{':
  137.             inbraces++;
  138.             continue;
  139.         case '(':
  140.             inparens++;
  141.             continue;
  142.         case '}':
  143.             inbraces--;
  144.             if(inbraces < 0) return(0);
  145.             continue;
  146.         case ')':
  147.             inparens--;
  148.             if(inparens < 0) {
  149.                 printf("too many ) ?");
  150.                 exit(1);
  151.             }
  152.             continue;
  153.         case '\n':
  154.             /* watch for #define at begin of line */
  155.             if((ch = nextchar()) == '#'){
  156.                 register char pch;
  157.                 /* skip until '\n' not preceded by '\\' */
  158.                 do {
  159.                     pch = ch;
  160.                     ch = nextchar();
  161.                 } while(ch != '\n' || pch == '\\');
  162.                 continue;
  163.             }
  164.             goto swi;
  165.         case ',':
  166.             if(!inparens && !inbraces){
  167.                 if(prefix && !string[prefix])
  168.                     string[0] = 0;
  169.                 if(stringseen) return(1);
  170.                 printf("unexpected ,\n");
  171.                 exit(1);
  172.             }
  173.             commaseen++;
  174.             continue;
  175.         case '\'':
  176.             if((ch = nextchar()) == '\\') ch = nextchar();
  177.             if(nextchar() != '\''){
  178.                 printf("strange character denotation?\n");
  179.                 exit(1);
  180.             }
  181.             continue;
  182.         case '"':
  183.             {
  184.                 register char *sp = string + prefix;
  185.                 register char pch;
  186.                 register int store = (inbraces || inparens)
  187.                     && !stringseen++ && !commaseen;
  188.                 do {
  189.                     pch = ch;
  190.                     ch = nextchar();
  191.                     if(store && sp < string+STRSZ)
  192.                         *sp++ = ch;
  193.                 } while(ch != '"' || pch == '\\');
  194.                 if(store) *--sp = 0;
  195.                 continue;
  196.             }
  197.         }
  198.     }
  199. }
  200.  
  201. capitalize(sp) register char *sp; {
  202.     if('a' <= *sp && *sp <= 'z') *sp += 'A'-'a';
  203. }
  204.  
  205. letter(ch) register char ch; {
  206.     return( ('a' <= ch && ch <= 'z') ||
  207.         ('A' <= ch && ch <= 'Z') );
  208. }
  209.  
  210. digit(ch) register char ch; {
  211.     return( '0' <= ch && ch <= '9' );
  212. }
  213.  
  214.